<?php
namespace app\client\lib;
use OSS\OssClient;
use OSS\Core\OssException;

class Alioss {
    const ACCESS_KEY_ID = 'LTAIoLK1RamE6JP2';
    const ACCESS_KEY_SECRET = 'em9eNzuYhy9wZnkPocL8vs76fek1K6';
    
    private $ossClient  = null;
    
    public function __construct($city = 'shenzhen') {
        $endpoint = "http://oss-cn-{$city}.aliyuncs.com";
        $this->ossClient = new OssClient(self::ACCESS_KEY_ID, self::ACCESS_KEY_SECRET, $endpoint);
    }
    
    /**
     * 创建存储空间
     * **/
    public function createBucket($bucketName) {
        try {
            $result = $this->ossClient->createBucket($bucketName, OssClient::OSS_ACL_TYPE_PUBLIC_READ, [
                OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_STANDARD
            ]);
            
            return true;
            
        } catch (\Exception $e) {
            return false;
        }
        
    }
    
    /**
     * 判断存储空间是否已存在
     * **/
    public function doesBucketExist($bucketName) {
        try {
            return $this->ossClient->doesBucketExist($bucketName);
        } catch (\Exception $e) {
            return false;
        }
    }
    
    
    /**
     * 上传文件
     * **/
    public function uploadFile($bucket, $fileName, $filePath) {
        try {
            $rs = $this->ossClient->uploadFile($bucket, $fileName, $filePath);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
}









